//! # 构建器核心接口定义
//! 
//! 本模块定义了RustCloud框架构建器的核心接口，包括基础构建器和配置构建器接口。

use crate::config::*;
use crate::framework::RustCloudFramework;
use rustcloud_core::{ServiceResult, ServiceError};
use rustcloud_registry::ServiceRegistry;
use rustcloud_loadbalancer::LoadBalancerStrategy;
use rustcloud_circuit_breaker::CircuitBreaker;
use rustcloud_metrics::MetricsCollector;
use std::sync::Arc;
use std::collections::HashMap;

/// 配置构建器trait
/// 
/// 为特定配置类型提供构建器功能
#[async_trait::async_trait]
pub trait ConfigurationBuilder<T>: Send + Sync {
    /// 构建配置对象
    fn build(self) -> ServiceResult<T>;
}

/// RustCloud框架构建器接口
/// 
/// 定义了构建RustCloud框架实例的核心接口
#[async_trait::async_trait]
pub trait RustCloudBuilderTrait: Send + Sync {
    /// 构建RustCloud框架实例
    async fn build(&self) -> ServiceResult<RustCloudFramework>;
    
    /// 获取配置
    fn get_config(&self) -> &RustCloudConfig;
    
    /// 设置服务配置
    fn with_service_config(&mut self, config: ServiceConfig) -> &mut Self;
    
    /// 设置服务版本
    fn with_version(&mut self, version: &str) -> &mut Self;
    
    /// 设置运行环境
    fn with_environment(&mut self, env: &str) -> &mut Self;
    
    /// 设置服务端口
    fn with_port(&mut self, port: u16) -> &mut Self;
    
    /// 添加服务标签
    fn with_tag(&mut self, key: &str, value: &str) -> &mut Self;
    
    /// 设置注册中心
    fn with_registry(&mut self, registry_url: &str) -> &mut Self;
    
    /// 设置注册中心实例
    fn with_registry_instance(&mut self, registry: Arc<dyn ServiceRegistry>) -> &mut Self;
    
    /// 设置Nacos注册中心
    fn with_nacos(&mut self, endpoints: Vec<String>, namespace: Option<String>) -> &mut Self;
    
    /// 设置Consul注册中心
    fn with_consul(&mut self, endpoints: Vec<String>) -> &mut Self;
    
    /// 设置负载均衡策略
    fn with_load_balancer(&mut self, strategy: &str) -> &mut Self;
    
    /// 设置负载均衡策略实例
    fn with_load_balancer_instance(&mut self, strategy: Arc<dyn LoadBalancerStrategy>) -> &mut Self;
    
    /// 启用轮询负载均衡
    fn with_round_robin(&mut self) -> &mut Self;
    
    /// 启用随机负载均衡
    fn with_random(&mut self) -> &mut Self;
    
    /// 启用加权轮询负载均衡
    fn with_weighted_round_robin(&mut self, weights: HashMap<String, u32>) -> &mut Self;
    
    /// 启用熔断器
    fn with_circuit_breaker(&mut self) -> &mut Self;
    
    /// 设置熔断器配置
    fn with_circuit_breaker_config(&mut self, config: CircuitBreakerConfig) -> &mut Self;
    
    /// 设置熔断器实例
    fn with_circuit_breaker_instance(&mut self, circuit_breaker: Arc<CircuitBreaker>) -> &mut Self;
    
    /// 启用指标收集
    fn with_metrics(&mut self) -> &mut Self;
    
    /// 设置指标配置
    fn with_metrics_config(&mut self, config: MetricsConfig) -> &mut Self;
    
    /// 设置指标收集器实例
    fn with_metrics_collector(&mut self, collector: Arc<dyn MetricsCollector>) -> &mut Self;
    
    /// 启用Prometheus指标导出
    fn with_prometheus(&mut self, port: Option<u16>) -> &mut Self;
    
    /// 启用分布式追踪
    fn with_tracing(&mut self) -> &mut Self;
    
    /// 设置追踪配置
    fn with_tracing_config(&mut self, config: TracingConfig) -> &mut Self;
    
    /// 设置Jaeger追踪
    fn with_jaeger(&mut self, endpoint: &str, service_name: Option<&str>) -> &mut Self;
    
    /// 设置日志配置
    fn with_logging_config(&mut self, config: LoggingConfig) -> &mut Self;
    
    /// 设置日志级别
    fn with_log_level(&mut self, level: &str) -> &mut Self;
    
    /// 启用网关
    fn with_gateway(&mut self, port: u16) -> &mut Self;
    
    /// 设置网关配置
    fn with_gateway_config(&mut self, config: GatewayConfig) -> &mut Self;
    
    /// 启用代理
    fn with_proxy(&mut self) -> &mut Self;
    
    /// 设置代理配置
    fn with_proxy_config(&mut self, config: ProxyConfig) -> &mut Self;
    
    /// 设置TLS配置
    fn with_tls(&mut self, cert_file: &str, key_file: &str, ca_file: Option<&str>) -> &mut Self;
    
    /// 设置性能配置
    fn with_performance_config(&mut self, config: PerformanceConfig) -> &mut Self;
    
    /// 设置连接池配置
    fn with_connection_pool(&mut self, max_connections: u32, min_connections: u32) -> &mut Self;
    
    /// 设置线程池配置
    fn with_thread_pool(&mut self, core_threads: u32, max_threads: u32) -> &mut Self;
    
    /// 添加自定义组件
    fn with_custom_component<T: std::any::Any + Send + Sync>(
        &mut self,
        name: &str,
        component: T,
    ) -> &mut Self;
    
    /// 从环境变量合并配置
    fn merge_from_env(&mut self) -> ServiceResult<&mut Self>;
}